const dao = require('./base');

module.exports = {

    transactionList: function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from ex_transaction a 
        left join ex_block b on a.blockNumber = b.number 
        where 1=1`;
        let pre = [];
        if (condition.transactionHash !== undefined && condition.transactionHash !== '') {
            sql += ` and a.transactionHash = ?`;
            pre.push(condition.transactionHash);
        }
        if (condition.blockHash !== undefined && condition.blockHash !== '') {
            sql += ` and a.blockHash = ?`;
            pre.push(condition.blockHash);
        }
        if (condition.blockNumber !== undefined && condition.blockNumber !== '') {
            sql += ` and a.blockNumber = ?`;
            pre.push(condition.blockNumber);
        }
        if (condition.start !== undefined && condition.start !== '') {
            sql += ` and a.blockNumber > ?`;
            pre.push(condition.start);
        }
        if (condition.end !== undefined && condition.end !== '') {
            sql += ` and a.blockNumber <= ?`;
            pre.push(condition.end);
        }
        if (condition.from !== undefined && condition.from !== '') {
            sql += ` and a.from = ?`;
            pre.push(condition.from);
        }
        if (condition.to !== undefined && condition.to !== '') {
            sql += ` and a.to = ?`;
            pre.push(condition.to);
        }
        if (condition.status !== undefined && condition.status !== '') {
            sql += ` and a.status = ?`;
            pre.push(condition.status);
        }
        if (condition.all !== undefined && condition.all !== '') {
            sql += ` and (a.from = ? or a.to = ?)`;
            pre.push(condition.all);
            pre.push(condition.all);
        }
        sql = dao.by(sql, by);
        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

    transactionAdd: function (fields = {}) {
        let sql = "INSERT INTO `ex_transaction` (`transactionHash`, `blockHash`, `transactionIndex`, `contractAddress`, " +
            "`gasUsed`, `logsBloom`, `logs`, `confirmations`, `blockNumber`, `cumulativeGasUsed`, " +
            "`status`, `byzantium`, `from`, `to`, `data`, `value`, `raw`, `creates`" +
            ") VALUES " +
            "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        let pre = [];
        pre.push(fields.transactionHash);
        pre.push(fields.blockHash);
        pre.push(fields.transactionIndex);
        pre.push(fields.contractAddress);
        pre.push(fields.gasUsed);
        pre.push(fields.logsBloom);
        pre.push(fields.logs);
        pre.push(fields.confirmations);
        pre.push(fields.blockNumber);
        pre.push(fields.cumulativeGasUsed);
        pre.push(fields.status);
        pre.push(fields.byzantium);
        pre.push(fields.from);
        pre.push(fields.to);
        pre.push(fields.data);
        pre.push(fields.value);
        pre.push(fields.raw);
        pre.push(fields.creates);

        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            console.log(pre);
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

};
